
#include <BEvent>
#include "member_BEvent.h"

using namespace BWE;

#define member					(*(member_BEvent*)_ptr)
#define member_allocate()		_ptr = new member_BEvent(this)
#define member_release()		delete (member_BEvent*)_ptr

BEvent::BEvent()
{
	member_allocate();
}
BEvent::BEvent(Event type)
{
	member_allocate();
	member.type = type;
}
BEvent::BEvent(Event type, const BValue& value)
{
	member_allocate();
	member.type = type;
	member.value = value;
}
BEvent::BEvent(const BEvent& other)
{
	member_allocate();
	(*this) = other;
}
BEvent::~BEvent()
{
	member_release();
}

void BEvent::reset()
{
	member.time = bTime();
	member.type = Event_None;
	member.accepted = false;
	member.result.clear();
	member.focusInWidget = 0;
	member.focusOutWidget = 0;
	member.focused = false;
	member.key = Key_None;
}
void BEvent::reset(Event type)
{
	member.time = bTime();
	member.type = type;
	member.value.clear();
	member.accepted = false;
	member.result.clear();
	member.focusInWidget = 0;
	member.focusOutWidget = 0;
	member.focused = false;
	member.key = Key_None;
}
void BEvent::reset(Event type, const BValue& value)
{
	member.time = bTime();
	member.type = type;
	member.value = value;
	member.accepted = false;
	member.result.clear();
	member.focusInWidget = 0;
	member.focusOutWidget = 0;
	member.focused = false;
	member.key = Key_None;
}

void BEvent::setType(Event type)
{
	member.type = type;
}
Event BEvent::type() const
{
	return member.type;
}

void BEvent::setValue(const BValue& value)
{
	member.value = value;
}
const BValue& BEvent::value() const
{
	return member.value;
}

void BEvent::accept() const
{
	member.accepted = true;
}
void BEvent::accept(const BValue& result) const
{
	member.accepted = true;
	member.result = result;
}
const BValue& BEvent::result() const
{
	return member.result;
}
bool BEvent::accepted() const
{
	return member.accepted;
}

BReal BEvent::time() const
{
	return member.time;
}
BReal BEvent::now() const
{
	return bTime();
}

bool BEvent::keyState(Key key) const
{
	if (member.window && key)
	{
		return member.window->keyStates(key);
	}
	return false;
}

const BEvent& BEvent::operator = (const BEvent& other)
{
	if (this != &other)
	{
		member_BEvent* omem = event_member(&other);

		member.type = omem->type;
		member.time = omem->time;
		member.value = omem->value;
		member.window = omem->window;

		member.focusInWidget = omem->focusInWidget;
		member.focusOutWidget = omem->focusOutWidget;
		member.focused = omem->focused;
		member.key = omem->key;

		member.button = omem->button;
		member.pos = omem->pos;
		member.absolutePos = omem->absolutePos;
		member.globalPos = omem->globalPos;
		member.wheel = omem->wheel;
	}
	return *this;
}
